package com.gitee.srurl.core.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author zxw
 * @date 2023/5/2 22:37
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public abstract class CryptoUtil {

    /**
     * 密码加密
     *
     * @param plaintextPassword 传入的明文密码
     * @return 加密后的密码
     */
    public static String passwordCrypto(final String plaintextPassword) {

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(plaintextPassword);
    }

    /**
     * 密码匹配
     *
     * @param plaintextPassword 传入的明文密码
     * @param encodedPassword   加密密码
     * @return 是否匹配
     */
    public static boolean passwordMatches(final String plaintextPassword, final String encodedPassword) {

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.matches(plaintextPassword, encodedPassword);
    }
}
